package com.qiwx.pigeon.activity.stock;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DateTimeUtils;
import com.qiwx.pigeon.Utils.LogUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.Utils.NumberUtil;
import com.qiwx.pigeon.Utils.StringUtil;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseMVPActivity;
import com.qiwx.pigeon.data.EnumUint;
import com.qiwx.pigeon.data.network.BaseResponse;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.data.vo.stock.AddGoodBean;
import com.qiwx.pigeon.data.vo.stock.GoodCategory;
import com.qiwx.pigeon.presenter.GoodCatePresenter;
import com.qiwx.pigeon.viewinterface.ICommonView;
import com.qiwx.pigeon.widget.ArrowView;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020/9/2 5:00 PM
 * 添加货品
 **/
public class AddGoodActivity extends BaseMVPActivity<ICommonView<GoodCategory>, GoodCatePresenter> implements ICommonView<GoodCategory> {

    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.title_right)
    TextView titleRight;
    @BindView(R.id.llTitle)
    LinearLayout llTitle;
    @BindView(R.id.edt_good_name)
    EditText edtGoodName;
    @BindView(R.id.edt_unit)
    EditText edtUnit;
    @BindView(R.id.edt_amount)
    EditText edtAmount;
    @BindView(R.id.txt_add_date)
    TextView txtAddDate;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    GoodCategory goodCategory;
    int addAmount;


    @Override
    public GoodCatePresenter createPresenter() {
        return new GoodCatePresenter(new WeakReference(this));
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_good_layout;
    }

    @Override
    public void initAfterOnCreate() {
        initView();
    }

    private void initView() {
        goodCategory = (GoodCategory) getIntent().getSerializableExtra("obj");
        if (goodCategory == null) {
            finish();
            return;
        }
        titleReturn.changeArrowLeft();
        titleContent.setText(goodCategory.getName());
        edtGoodName.setText(goodCategory.getName());
        edtUnit.setText(goodCategory.getUnit_name());
        txtAddDate.setText(DateTimeUtils.getCurrentDateStr());
    }


    public boolean checkParams() {
        addAmount = NumberUtil.parseInt(edtAmount.getText().toString());
        if (addAmount == 0) {
            MyToast.showCenterShort("数量不能为0");
            return false;
        }
        if (addAmount > goodCategory.getQuantity()) {
            MyToast.showCenterShort("数量不能大于库存");
            return false;
        }

        return true;
    }

    private void submitData() {
        AddGoodBean bean = new AddGoodBean();
        bean.setAdd_date(txtAddDate.getText().toString());
        bean.setAmount(addAmount);
        bean.setF_id(goodCategory.getF_id());
        bean.setType(0);
        bean.setGood_id(goodCategory.getId());
        mPresenter.addGood(bean);
    }


    @Override
    public void onAddSuccess(BaseResponse response) {
        MyToast.showCenterShort("添加成功");
        finish();
//        edtCompanyName.setText("");
//        edtCompanyAdress.setText("");
//        edtCompanyPhone.setText("");

    }

    @Override
    public void onDeleteSuccess(BaseResponse response) {

    }

    @Override
    public void onQueryListSuccess(PageInfo<GoodCategory> list) {

    }

    @OnClick({R.id.title_return, R.id.txt_add_date, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_return:
                finish();
                break;
            case R.id.btn_submit:
                if (checkParams()) {
                    submitData();
                }
                break;
            case R.id.txt_add_date:
                break;
        }
    }
}
